package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaUtil;
import com.hemaapp.hm_FrameWork.dialog.HemaButtonDialog;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.Notice;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 类描述：
 * 创建人：GuiyouZhang
 * 创建时间：2016/8/2 20:40
 * 修改备注：
 */
public class NoticeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private ArrayList<Notice> datas;
    private OnNoticeClickListener itemClickListener;

    public NoticeAdapter(Context context, ArrayList<Notice> datas) {
        this.mContext = context;
        this.datas = datas;
    }

    public void setItemClickListener(OnNoticeClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == -1) {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.listitem_empty_base, parent, false);
            return new EmptyViewHolder(itemView);
        } else {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_notice, parent, false);
            return new ViewHolder(itemView);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (datas.size() == 0) {
            if (holder instanceof EmptyViewHolder)
                ((EmptyViewHolder) holder).tvEmtpy.setText("暂无消息记录");
        } else {
            if (holder instanceof ViewHolder) {
                ((ViewHolder) holder).setData(position);
            }
        }
    }

    @Override
    public int getItemCount() {
        return datas.size() == 0 ? 1 : datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (datas.size() == 0)
            return -1;
        else
            return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.avatar)
        ImageView ivAvatar;
        @Bind(R.id.point)
        ImageView point;
        @Bind(R.id.nickname)
        TextView nickname;
        @Bind(R.id.date)
        TextView date;
        @Bind(R.id.content)
        TextView content;

        int position = -1;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (itemClickListener != null && position != -1)
                        itemClickListener.onClickListener(position);
                }
            });
            view.setOnLongClickListener(new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {
                    final Notice notice1 = (Notice) v.getTag();
                    HemaButtonDialog dialog = new HemaButtonDialog(mContext);
                    dialog.setText("确定要删除该信息？");
                    dialog.setLeftButtonText("取消");
                    dialog.setRightButtonText("确定");
                    dialog.setButtonListener(new HemaButtonDialog.OnButtonListener() {

                        @Override
                        public void onRightButtonClick(HemaButtonDialog dialog) {
                            dialog.cancel();
                            if (itemClickListener != null && position != -1)
                                itemClickListener.onLongClickListener(position);
                        }

                        @Override
                        public void onLeftButtonClick(HemaButtonDialog dialog) {
                            dialog.cancel();
                        }
                    });
                    return false;
                }
            });
        }

        public void setData(int position) {
            this.position = position;
            Notice data = datas.get(position);
            ImageUtils.loadAvatar(mContext, data.getAvatar(), ivAvatar);
            nickname.setText(data.getNickname());
            date.setText(data.getRegdate());
            //显示帖子内容，并将其中emoji表情进行转换
            HemaUtil.SetMessageTextView(mContext, content, data.getContent());
            if ("2".equals(data.getLooktype())) {//1未读 2已读
                point.setVisibility(View.GONE);
            } else {
                point.setVisibility(View.VISIBLE);
            }
        }
    }

    public interface OnNoticeClickListener {
        public void onClickListener(int position);
        public void onLongClickListener(int position);
    }
}
